﻿using ServerCrafter.ClassLibrary;
using ServerCrafter.ClassLibrary.ViewModels;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Data;

namespace ServerCrafter.WPF
{
    public partial class MainWindow
    {
        private void txtboxInstalledModsSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            var descriptor = new FilterDescriptor
            {
                Member = "Name",
                Operator = FilterOperator.Contains,
                Value = txtboxInstalledModsSearch.Text
            };
            gridviewInstalledMods.FilterDescriptors.Clear();
            gridviewInstalledMods.FilterDescriptors.Add(descriptor);
            gridviewInstalledCoreMods.FilterDescriptors.Clear();
            gridviewInstalledCoreMods.FilterDescriptors.Add(descriptor);
        }

        private void btnDeleteMod_Click(object sender, RoutedEventArgs e)
        {
            var modPath = Settings.ServerStartInfo.FileName + "mods/" + ((Button)sender).Tag + ".jar";
            if (!File.Exists(modPath)) modPath = modPath.TrimEnd(new[] { 'j', 'a', 'r' }) + "zip";
            File.Delete(modPath);

            ModManager.InstalledMods.CreateList();
            var installedMods = new VMInstalledMods();
            gridviewInstalledMods.ItemsSource = installedMods.Get();
        }

        private void btnDeleteCoreMod_Click(object sender, RoutedEventArgs e)
        {
            var coreModPath = Settings.ServerStartInfo.FileName + "coremods/" + ((Button)sender).Tag + ".jar";
            if (!File.Exists(coreModPath)) coreModPath = coreModPath.TrimEnd(new[] { 'j', 'a', 'r' }) + "zip";
            File.Delete(coreModPath);

            ModManager.InstalledCoreMods.CreateList();
            var installedCoreMods = new VMInstalledCoreMods();
            gridviewInstalledMods.ItemsSource = installedCoreMods.Get();
        }

        private void btnOpenCoreModsFolder_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(Settings.ServerStartInfo.FileName + @"coremods\");
        }

        private void btnOpenModsFolder_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(Settings.ServerStartInfo.FileName + @"mods\");
        }
    }
}